/*
*********************************
	PlovR Game Engine
*********************************

	By Julian Williams
	June 11, 2010
**********************************

	Description:

		Parallax Scrolling Class
	Get cool background movement 
	effects!
***********************************
*/
#include <SFML/Graphics.hpp>
#include <iostream>


using namespace std;

#include "Parallax.h"
#include "Camera.h"
#include "image_manager.h"
#include "app.h"

//static map scrollage
sf::Vector2f Parallax::map_scroll;




//Constructor
Parallax::Parallax(float scroll_speed, string location )
{
	Set_ScrollSpeed(scroll_speed);
	Load( location );
}//EoF



//Load the Image and set the initial positions of the backgrounds
bool Parallax::Load(string location)
{
	cout << "Loading Parallax Background from: " << location << endl;

	//Load image
	img = gImageManager.getResource( location );
	//Image.LoadFromFile(location);
	//Image.CreateMaskFromColor(sf::Color(255,0,255));

	//SetImage( *img );
//	SetPosition(img->GetWidth(), 0);

    if( img != NULL ) 
	{
		if(Get_ScrollSpeed() == 0)
		{
			SetImage( *img );

				if((img->GetWidth() < App::getInstance().Get_RenderWindow()->GetWidth() ) ||
					img->GetHeight() < App::getInstance().Get_RenderWindow()->GetHeight() )
				{
					Resize(App::getInstance().Get_RenderWindow()->GetWidth(), App::getInstance().Get_RenderWindow()->GetHeight());
				}
		}else{
		
				for(int i = 0; i < 4; i++)
				{
			
					back[i].SetImage( *img );

			//if not scrolling, and smaller than screen, enlarge
			

			//cout << "Set Image[" << i << "] \n";
				}
			}
		
    }else{

		cout << "Parallax Background at: " << location << "\nFailed to load!\n";
		return false;
	}

	//reset positions
		Reset();

	return true;
}//EoF




//Reset background positions
void Parallax::Reset()
{
	back[0].SetPosition(0, 0);
	back[1].SetPosition(img->GetWidth(), 0);
	back[2].SetPosition(0, img->GetHeight());
	back[3].SetPosition(img->GetWidth(), img->GetHeight());
}


//Update the image placement of the Parallax backgrounds
void Parallax::Update_Logic()
{

	//Grab the screen width/height
	const int Screen_Width = App::getInstance().Get_RenderWindow()->GetWidth();
	const int Screen_Height = App::getInstance().Get_RenderWindow()->GetHeight();

	if(scroll_speed != 0)
	{

		//Loop through the 4 background images
		for(int i = 0; i < 4; i++)
		{
			//Grab image coords
			int left = back[i].GetPosition().x;
			int right = back[i].GetPosition().x + img->GetWidth();

			int top = back[i].GetPosition().y;
			int down = back[i].GetPosition().y + img->GetHeight();



			//Update Position
			back[i].SetPosition(  back[i].GetPosition() -( Cam.Get_Scroll()  * scroll_speed));

	//   cout << "Camera Position: " << Cam.GetPosition().x << ", " << Cam.GetPosition().y << endl;
			
		//X Axis
			if(left > Screen_Width)
			{
				back[i].SetX( back[i].GetPosition().x - (img->GetWidth()*2));

			}else if( right < 0 )
			{
				back[i].SetX( back[i].GetPosition().x + (img->GetWidth()*2));
			}


		//Y Axis
			if( top > Screen_Height )
			{
				back[i].SetY( back[i].GetPosition().y - (img->GetHeight()*2));

			}else if( down < 0 )
			{
				back[i].SetY( back[i].GetPosition().y + (img->GetHeight()* 2) );
			}
		}
	}//end of speed check
}//EoF




//Draw the background to the screen
void Parallax::Draw()
{
	//Grab the screen width/height
	const int Screen_Width = App::getInstance().Get_RenderWindow()->GetWidth();
	const int Screen_Height = App::getInstance().Get_RenderWindow()->GetHeight();


	//check speed
	if(scroll_speed == 0)
	{
		App::getInstance().Get_RenderWindow()->Draw(*this);

	}else{

		for(int i = 0; i < 4; i++ )
		{
			//Grab image coords
			int left = back[i].GetPosition().x;
			int right = back[i].GetPosition().x + img->GetWidth();

			int top = back[i].GetPosition().y;
			int down = back[i].GetPosition().y + img->GetHeight();


		//Check to make sure the image is visabe on screen
			if(((left < Screen_Width ) && ( right > 0)) ||
				(top  < Screen_Height) && ( down  > 0))
			{
			//DRAW TO SCREEN YAYY!!!
				App::getInstance().Get_RenderWindow()->Draw( back[i] );
			}

		}
	}//end of speed check
}//EoF




//Set Scroll Displacement
void Parallax::Set_ScrollX(float vx)
{
	map_scroll.x = vx;
}
void Parallax::Set_ScrollY(float vy)
{
	map_scroll.y = vy;
}
//Fuck.
		



